package com.example.Arithmetic.Arithmetic;

import java.util.LinkedList;
import java.util.List;

/**
 * 日期：2023/12/29
 * 时间：13:44
 * 描述：图的BFS广度优先遍历
 */
public class FigureBfs {
    public static void main(String[] args) {
        Vertex v1 = new Vertex("v1");
        Vertex v2 = new Vertex("v2");
        Vertex v3 = new Vertex("v3");
        Vertex v4 = new Vertex("v4");
        Vertex v5 = new Vertex("v5");
        Vertex v6 = new Vertex("v6");

        v1.edges = List.of(new Edge(v3), new Edge(v2), new Edge(v6));
        v2.edges = List.of(new Edge(v4));
        v3.edges = List.of(new Edge(v4), new Edge(v6));
        v4.edges = List.of(new Edge(v5));
        v5.edges = List.of();
        v6.edges = List.of(new Edge(v5));

        dfs1(v1);
    }

//    队列遍历
    private static void dfs1(Vertex v1) {
        LinkedList<Vertex> list = new LinkedList();
        list.add(v1);
        v1.visited = true;
        while (!list.isEmpty()) {
            Vertex poll = list.poll();
            System.out.println(poll.name);
            for (Edge e : poll.edges) {
                if (!e.linked.visited) {
                    e.linked.visited = true;
                    list.offer(e.linked);
                }
            }
        }
    }
}
